package com.touch.jgloo;
import java.awt.event.*;
import java.util.Vector;
import javax.media.opengl.*;
import com.sun.opengl.util.Animator;
import com.touch.jgloo.core.JGLBase;
/**
 * The RenderCanvas is an Object that manages the GLCanvas and makes it more accessable by assuming that the user will use Ortho as well as mouse/keyboard interfaces.
 * You need to add this canvas to a Java pane/form by adding the canvas obtained by "getCanvas()"
 * JGLRenderCanvas also effectively manages JGLComponents and is recommended to be used instead of JGLInterface when it comes to using JGLComponents.
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r419
 * @deprecated Use JGLDefaultView/JGLOrthoView and GLCanvas instead
 * @see com/touch/Licence 
 */
public abstract class JGLRenderCanvas extends JGLBase implements GLEventListener, MouseListener, KeyListener
{	
	private static final long serialVersionUID = 1L;
	private final Vector <MouseListener> MouseListeners = new Vector <MouseListener>();
	private final Vector <KeyListener> KeyListeners = new Vector <KeyListener>();
	private final GLCanvas canvas;
	/**
	 * Creates the JGLRenderCanvas Object. This also prepares the GLCanvas and Animator and adds the mouse/key listeners to them.	 * 
	 */
	public JGLRenderCanvas()
	{
		canvas = new GLCanvas();
		final Animator animator = new Animator(canvas);		
		canvas.addGLEventListener(this);
		canvas.addMouseListener(this);
		canvas.addKeyListener(this);		
		animator.start();
	}
	/**
	 * Returns the GLCanvas object that is used to draw any OpenGL events to. This is only recommended to be used when adding this to another container.
	 * @return
	 */
	public final synchronized GLCanvas getCanvas()
	{
		return canvas;
	}
	/**
	 * Adds a MouseListener to the Canvas. This is abstractly called and piped through the form framework.
	 * @param mListener
	 */
	public final synchronized void addMouseListener(final MouseListener mListener)
	{
		this.MouseListeners.add(mListener);
	}
	/**
	 * Adds a key listener to the canvas. This is used primarily for JGLComponents that contain mouse/key events.
	 * @param kListener
	 */
	public final synchronized void addKeyListener(final KeyListener kListener)
	{
		this.KeyListeners.add(kListener);
	}		
	/** 
	 * display(GL) draws the gl events. It is called by this class's display(GLAutoDrawable) which sets up the display before drawing.
	 * @param gl GL link
	 */
	public abstract void display();
	public final void display(final GLAutoDrawable drawable) 
	{
		final GL gl = drawable.getGL();
		GL_INSTANCE = gl;
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);	
		display();
	}

	public final void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) {}

	/** 
	 * init(GL) is called from the method init(GLAutoDrawable). It finishes the initialization process.
	 * @param gl
	 */
	public abstract void init();
	public final void init(final GLAutoDrawable drawable) 
	{		
		//get the GL object
		final GL gl = drawable.getGL();
		GL_INSTANCE = gl;
		//set the shading mode
		gl.glShadeModel(GL.GL_SMOOTH);
		//Set the background color to black
		gl.glClearColor(0f, 0f, 0f, 0f);
		//Set the depth to 1f
		gl.glClearDepth(1.0f);
		//Enable the use of the z-axis
		gl.glEnable(GL.GL_DEPTH_TEST);
		//Enable using textures
		gl.glEnable(GL.GL_TEXTURE_2D);

		//eh more depth enabling
		gl.glDepthFunc(GL.GL_LEQUAL);
		//more high-quality
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		//more high-quality
		gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);		
		
		init();
	}
	public final void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width,
			final int height) 
	{
		//get the GL Object
		final GL gl = drawable.getGL();
		GL_INSTANCE = gl;
		//only draw when the screen re-syncs
		gl.setSwapInterval(1);
		//set the view size of the GL instance
		gl.glViewport(0, 0, width, height);
		//swich to projection mode
		gl.glMatrixMode(GL.GL_PROJECTION);
		//load the identity matrix
		gl.glLoadIdentity();
		//define the dimensions (600x480x50)
		gl.glOrtho(0d, 640d, 480d, 0d, 0d, 50d);
		//switch back to model mode
		gl.glMatrixMode(GL.GL_MODELVIEW);
		//reload the identity matrix
		gl.glLoadIdentity();		
	}
	public final void mouseClicked(final MouseEvent e) 
	{
		for(int i = 0; i < MouseListeners.size(); i++)		
			MouseListeners.elementAt(i).mouseClicked(e);		
	}
	public final void mouseEntered(final MouseEvent e) 	
	{
		for(int i = 0; i < MouseListeners.size(); i++)
			MouseListeners.elementAt(i).mouseEntered(e);
	}
	public final void mouseExited(final MouseEvent e) 	
	{
		for(int i = 0; i < MouseListeners.size(); i++)
			MouseListeners.elementAt(i).mouseExited(e);
	}
	public final void mousePressed(final MouseEvent e)
	{
		for(int i = 0; i < MouseListeners.size(); i++)
			MouseListeners.elementAt(i).mousePressed(e);
	}
	public final void mouseReleased(final MouseEvent e) 
	{
		for(int i = 0; i < MouseListeners.size(); i++)
			MouseListeners.elementAt(i).mouseReleased(e);
	}
	public final void keyPressed(final KeyEvent e) 
	{
		for(int i = 0; i < KeyListeners.size(); i++)
			KeyListeners.elementAt(i).keyPressed(e);
	}
	public final void keyReleased(final KeyEvent e) 
	{
		for(int i = 0; i < KeyListeners.size(); i++)
			KeyListeners.elementAt(i).keyReleased(e);
	}
	public final void keyTyped(final KeyEvent e) 
	{	
		for(int i = 0; i < KeyListeners.size(); i++)
			KeyListeners.elementAt(i).keyTyped(e);
	}	
}
